import { getUser, removeUser } from "../../dao/auth"

const state = {
  user: null,
  warehouseid: ''
}

const getters = {
  user: state => state.user,
}

const mutations = {
  SET_USER(state, val) {
    state.user = val
  }
}

const actions = {
  loadUser({ commit }) {
    return new Promise((resolve, reject) => {
      getUser().then(res => {
        if (res) {
          commit('SET_USER', res)
          resolve('ok')
        } else {
          reject()
        }
      }).catch(e => {
        reject(e)
      })
    })
  },
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      removeUser().then(() => {
        commit('SET_USER', null)
        resolve()
      }).catch(e => {
        reject(e)
      })
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
